package lihua.robot.utils;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lihua.robot.controller.BlocksController;
import lihua.robot.pojo.Device;
import lihua.robot.pojo.ExpiredOrders;
import lihua.robot.service.DeviceService;
import lihua.robot.service.ExpiredOrdersService;
import lihua.robot.service.FallingTimeService;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static lihua.robot.utils.RedisUtil.getWarehouselocation;

@Component
public class TransportationConditionsUtil {
    //满桶条件

    public static void fullBarrelCondition(Integer dqcd,Integer before_dqcd,int devSn){
        FallingTimeService fallingTimeService = (FallingTimeService) ApplicationContextUtil.getBean(FallingTimeService.class);
        //判断是否为满桶 0：空桶 1：半桶 2：满桶 3:落纱中
        if( dqcd  < before_dqcd ){
            RedisUtil.set0(devSn);
            DeviceService deviceService = (DeviceService)ApplicationContextUtil.getBean(DeviceService.class);
            ExpiredOrdersService expiredOrdersService = (ExpiredOrdersService)ApplicationContextUtil.getBean(ExpiredOrdersService.class);
            LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Device::getWorkshop,3)
                    .eq(Device::getDevsn,devSn);
            Device de = deviceService.getBaseMapper().selectOne(wrapper);

            switch (de.getState()){
                case 0:
                case 2:
                    de.setState(1);
                    de.setUpdateTime(DateUtil.date());
                    deviceService.getBaseMapper().updateById(de);
                    fallingTimeService.saveFallingTime(dqcd, before_dqcd, devSn, 3, null, 0, null);
                    break;
                case 1:
                    BlocksController blocksController = new BlocksController();
                    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
                    int devsn = devSn;
                    if(getWarehouselocation(1,determineTheHandoverArea(devsn))== null ||
                            getWarehouselocation(2,determineTheHandoverArea(devsn)) == null){
                        ExpiredOrders expiredOrders = new ExpiredOrders();
                        expiredOrders.setDesvn(String.valueOf(devSn));
                        expiredOrders.setUpdateTime(DateUtil.date());
                        expiredOrdersService.save(expiredOrders);
                        break;
                    }
                    de.setState(3);
                    de.setUpdateTime(DateUtil.date());
                    deviceService.getBaseMapper().updateById(de);


                    // 大桶运单1
                    executorService.schedule(() -> {
                        de.setState(2);
                        de.setUpdateTime(DateUtil.date());
                        deviceService.getBaseMapper().updateById(de);

                        System.out.println("设备"+devSn+"--"+DateUtil.date()+"AMB-0301-开始执行-1");
                        String MAP = getWarehouselocation(1,determineTheHandoverArea(devsn));
                        System.out.println(MAP);
                        String ydid = String.valueOf(blocksController.transportFullBarrels(String.valueOf(devsn),MAP).getData());
                        RedisUtil.setKW1(MAP);
                        fallingTimeService.saveFallingTime(dqcd, before_dqcd, devsn, 3, ydid, 0, null);
                        try {
                            setKW0(MAP,ydid);
                        } catch (UnsupportedEncodingException e) {
                            throw new RuntimeException(e);
                        }
                    }, 30, TimeUnit.MINUTES);

                    // 大桶运单2
                    executorService.schedule(() -> {

                        System.out.println("设备"+devSn+"--"+DateUtil.date()+"AMB-0301-开始执行-2");
                        String KAP = getWarehouselocation(2,determineTheHandoverArea(devsn));
                        System.out.println(KAP);
                        String ydid = String.valueOf(blocksController.transportEmptyDrums(String.valueOf(devsn), KAP).getData());
                        RedisUtil.setKW1(KAP);
                        fallingTimeService.saveFallingTime(dqcd, before_dqcd, devsn, 3, ydid, 0, null);
                        try {
                            setKW0(KAP,ydid);
                        } catch (UnsupportedEncodingException e) {
                            throw new RuntimeException(e);
                        }
                    }, 32, TimeUnit.MINUTES);

                    break;
                default:
                    de.setState(0);
                    de.setUpdateTime(DateUtil.date());
                    deviceService.getBaseMapper().updateById(de);
                    break;
            }
        }

        if(dqcd>2000 && dqcd <4000){
            BlocksController blocksController = new BlocksController();
            ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
            int devsn = devSn;
            if(RedisUtil.getXT(devsn)==null){
                int time = 0;
                if(devsn<57&&dqcd>30){
                    time = 15;
                }
                if(devsn<30&&devsn>0){
                    time = 30;
                }
                if(!(getWarehouselocation(3,determineTheHandoverArea(devsn))== null ||
                        getWarehouselocation(4,determineTheHandoverArea(devsn)) == null)){
                    RedisUtil.setXT(devsn);
                    executorService.schedule(() -> {
                        System.out.println("设备"+devSn+"--"+DateUtil.date()+"AMB-0302-开始执行-1");

                        String XMAP = getWarehouselocation(3,determineTheHandoverArea(devsn));
                        System.out.println(XMAP);
                        String xydid = String.valueOf(blocksController.s_transportFullBarrels(devsn, XMAP).getData());
                        //订单接收预约情况置为1
                        RedisUtil.setKW1(XMAP);
                        fallingTimeService.saveFallingTime(dqcd, before_dqcd, devsn, 3, xydid, 1, 0);
                        // 订单完成后预约情况置为0
                        try {
                            setKW0(XMAP,xydid);
                        } catch (UnsupportedEncodingException e) {
                            throw new RuntimeException(e);
                        }

                    }, 5, TimeUnit.MINUTES);

                    //小桶运单2
                    executorService.schedule(() -> {
                        System.out.println("设备"+devSn+"--"+DateUtil.date()+"AMB-0302-开始执行-2");

                        String XKAP = getWarehouselocation(4,determineTheHandoverArea(devsn));
                        System.out.println(XKAP);
                        String xydid = String.valueOf(blocksController.s_transportEmptyDrums(devsn, XKAP).getData());
                        RedisUtil.setKW1(XKAP);
                        fallingTimeService.saveFallingTime(dqcd, before_dqcd, devsn, 3, xydid, 1, 0);
                        try {
                            setKW0(XKAP,xydid);
                        } catch (UnsupportedEncodingException e) {
                            throw new RuntimeException(e);
                        }

                    }, time, TimeUnit.MINUTES);
                }
                }


        }



    }

    public static int determineTheHandoverArea(int deSvn) {
        if(deSvn>=1 && deSvn<9){
            return 2;
        }
        return 1;
    }

    /**
     * 轮询订单完成情况
     * @param MAP
     * @param ydid
     */
    public static void setKW0(String MAP,String ydid) throws UnsupportedEncodingException {

        ImprovedPollerUtil.pollOrderStatus(ydid,5,4 * 60000,MAP);

    }



}
